package com.scoy.honeymei.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.home.FilmList0Activity;
import com.scoy.honeymei.activity.home.FilmListActivity;
import com.scoy.honeymei.adapter.FilmNewHListAdapter;
import com.scoy.honeymei.adapter.FilmNewVListAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.bean.FilmListBean;
import com.scoy.honeymei.dialog.ShareDialog;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: FilmFragment$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 上午 9:34$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 上午 9:34$
 * @UpdateRemark: 更新内容
 */
public class Film0Fragment extends BaseFragment {
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private int type;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<FilmBean> datalist;

    private long cTime;

    private int pageInt;
    private int isV;
    private FilmNewHListAdapter hfilmAdapter;
    private FilmList0Activity mActivity;
    private int cinemaId;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            cTime = getArguments().getLong("cTime");
            cinemaId = getArguments().getInt("cinemaId");
        }
    }

    public static Film0Fragment newInstance(int type, long cTime, int cinemaId) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putLong("cTime", cTime);
        args.putInt("cinemaId", cinemaId);
        Film0Fragment fragment = new Film0Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (FilmList0Activity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();

        httpFilmlist(0);

        return view;
    }

    public void setType(int type){
        this.isV = type;
        if (isV == 1) {
            normalRv.setPadding((int) getActivity().getResources().getDimension(R.dimen.dp_8), 0, (int) getActivity().getResources().getDimension(R.dimen.dp_8), 0);
            normalRv.setLayoutManager(new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false));
        } else {
            normalRv.setPadding(0, 0, 0, 0);
            normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        }
        hfilmAdapter.setType(isV);
        hfilmAdapter.notifyDataSetChanged();
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();

        initHRV();

/*        mActivity.setOnChangeType(type -> {
            isV = type;
            if (isV == 1) {
                normalRv.setPadding((int) getActivity().getResources().getDimension(R.dimen.dp_8), 0, (int) getActivity().getResources().getDimension(R.dimen.dp_8), 0);
                normalRv.setLayoutManager(new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false));
            } else {
                normalRv.setPadding(0, 0, 0, 0);
                normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
            }
            hfilmAdapter.setType(isV);
            hfilmAdapter.notifyDataSetChanged();
        });*/

        norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            hfilmAdapter.clearData();
            httpFilmlist(0);

        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            if (pageInt == -1) {
                MyUtil.mytoast(mContext, getString(R.string.nomore));
            } else {
                httpFilmlist(pageInt);
            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initHRV() {
        hfilmAdapter = new FilmNewHListAdapter(mContext);
        if (isV == 1) {
            normalRv.setLayoutManager(new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false));
            normalRv.setPadding(0, 0, 0, 0);

        } else {
            normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
            normalRv.setPadding((int) getActivity().getResources().getDimension(R.dimen.dp_8), 0, (int) getActivity().getResources().getDimension(R.dimen.dp_8), 0);

        }
        normalRv.setAdapter(hfilmAdapter);
        hfilmAdapter.setType(isV);
        hfilmAdapter.setOnOneClick(pos -> {

            FilmBean movieBean = datalist.get(pos);
            Intent intentde = new Intent(mContext, FilmListActivity.class);
            intentde.putExtra("mid", movieBean.getMid());
            intentde.putExtra("pos", type);
            intentde.putExtra("cinemaId", cinemaId);
            intentde.putExtra("filmData", movieBean);
            startActivity(intentde);
        });

        hfilmAdapter.setOnTwoClick(pos -> {
            FilmBean movieBean = datalist.get(pos);
            ShareDialog.newInstance(movieBean.getFx_image(), movieBean.getTitle(),
                    movieBean.getFx_miaoshu(), movieBean.getFx_url())
                    .show(getFragmentManager(), "");

            //   MyUtil.mytoast0(mContext, "share+" + movieBean.getTitle());
        });

    }


    private void httpFilmlist(int myPage) {
        HttpParams params = new HttpParams();
        params.put("cinema_id", cinemaId);
        params.put("date", cTime);
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.CINEMA_FILM_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                FilmListBean filmListBean = new Gson().fromJson(o, FilmListBean.class);
                int next = filmListBean.getNext();
                List<FilmBean> movie = filmListBean.getMovie();
                pageInt = myPage + 1;
                if (next == 0) {
                    pageInt = -1;
                }
                datalist.addAll(movie);

                hfilmAdapter.addData(movie);
                List<FilmBean> data = hfilmAdapter.getData();
                nodataTv.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
